// 生成模拟设备数据
const generateEquipments = (count) => {
  const equipments = []
  const types = ['服务器', '路由器', '交换机', '防火墙', 'NAS', 'UPS']
  const statuses = ['online', 'offline', 'maintenance']
  const manufacturers = ['华为', '思科', '戴尔', 'HPE', '联想', '浪潮']
  
  for (let i = 1; i <= count; i++) {
    const type = types[Math.floor(Math.random() * types.length)]
    const manufacturer = manufacturers[Math.floor(Math.random() * manufacturers.length)]
    
    equipments.push({
      id: i,
      name: `${type}-${i.toString().padStart(3, '0')}`,
      type,
      serialNumber: `SN${Math.random().toString(36).substring(2, 10).toUpperCase()}`,
      ip: `192.168.${Math.floor(Math.random() * 255)}.${Math.floor(Math.random() * 255)}`,
      mac: Array(6).fill(0).map(() => Math.floor(Math.random() * 256).toString(16).padStart(2, '0')).join(':'),
      manufacturer,
      model: `Model-${Math.floor(Math.random() * 1000)}`,
      status: statuses[Math.floor(Math.random() * statuses.length)],
      location: `机房${Math.floor(Math.random() * 5) + 1}-${Math.floor(Math.random() * 50) + 1}号机柜`,
      installDate: new Date(Date.now() - Math.floor(Math.random() * 1000) * 86400000).toISOString().split('T')[0],
      maintainDate: new Date(Date.now() + Math.floor(Math.random() * 365) * 86400000).toISOString().split('T')[0]
    })
  }
  
  return equipments
}

// 设备相关的模拟数据
export function setupEquipmentMock(mock) {
  // 模拟的设备数据
  const equipments = generateEquipments(200)
  
  // 获取设备列表
  mock.onGet('/api/equipment/list').reply((config) => {
    const { page = 1, pageSize = 10, keyword = '', type = '' } = config.params || {}
    
    let filteredEquipments = [...equipments]
    
    // 按类型过滤
    if (type) {
      filteredEquipments = filteredEquipments.filter(
        equipment => equipment.type === type
      )
    }
    
    // 关键词过滤
    if (keyword) {
      filteredEquipments = filteredEquipments.filter(
        equipment => equipment.name.includes(keyword) || 
                    equipment.serialNumber.includes(keyword) || 
                    equipment.ip.includes(keyword)
      )
    }
    
    // 分页
    const total = filteredEquipments.length
    const start = (page - 1) * pageSize
    const end = start + Number(pageSize)
    const pageData = filteredEquipments.slice(start, end)
    
    return [
      200,
      {
        code: 200,
        message: 'success',
        data: {
          list: pageData,
          pagination: {
            page: Number(page),
            pageSize: Number(pageSize),
            total
          }
        }
      }
    ]
  })
  
  // 获取设备类型列表
  mock.onGet('/api/equipment/types').reply(() => {
    return [
      200,
      {
        code: 200,
        message: 'success',
        data: ['服务器', '路由器', '交换机', '防火墙', 'NAS', 'UPS']
      }
    ]
  })
  
  // 获取设备详情
  mock.onGet(/\/api\/equipment\/detail\/\d+/).reply((config) => {
    const id = parseInt(config.url.split('/').pop())
    const equipment = equipments.find(e => e.id === id)
    
    if (equipment) {
      return [
        200,
        {
          code: 200,
          message: 'success',
          data: equipment
        }
      ]
    } else {
      return [
        200,
        {
          code: 404,
          message: '设备不存在',
          data: null
        }
      ]
    }
  })
} 